﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class RushEliteBossIcon : MonoBehaviour
    {
        public TextureKit bossIcon;
        public UISprite bossIconDi;
        public UISprite bossIconFrame;
        public UILabel nameLabel;
        public GameObject selector;

        public GameObject closeMask;
        public UILabel openConditionLabel;

        public UIEventListener.VoidDelegate onIconClick;

        int limitFloorNum;
        void OnClick()
        {
            if (onIconClick != null)
            {
                onIconClick(gameObject);
            }
            
        }

        public string GetBossId()
        {
            return bossId;
        }

        public bool IsOpen()
        {
            return isOpen;
        }

        string bossId;

        bool isOpen;

        bool isPassed;

        public bool IsPassed()
        {
            return isPassed;
        }

        RushEliteBossPage bossPage;

        public void Init(ProtoBuf.Message.RushEliteBossInfo info, bool select, UIEventListener.VoidDelegate onClick, RushEliteBossPage page)
        {
            this.bossId = info.eliteTaskId;
            this.isOpen = info.isOpen > 0;
            this.isPassed = info.isSuccess > 0;
            this.onIconClick = onClick;
            this.bossPage = page;
            var db = Dict.Blo.DictRushBlo.GetTableRushEliteBoss(bossId);
            if (db != null)
            {
                bossIcon.iconType = TextureIcon.HERO;
                bossIcon.textureName = db.IconName;
                bossIconDi.spriteName = CommonMethod.GetQualityBack((QualityValue)db.BossQuality);
                bossIconFrame.spriteName = CommonMethod.GetQualityFrame((QualityValue)db.BossQuality);

                nameLabel.text = TableTextBlo.GetText(db.EliteBossName);

                int.TryParse(db.ConditionTaskId, out limitFloorNum);

                selector.SetActive(select);

                if (isOpen)
                {
                    closeMask.SetActive(false);
                }
                else
                {
                    closeMask.SetActive(true);
                    openConditionLabel.text = ParseBossOpenCondition();
                }
            }
        }

        public string ParseBossOpenCondition()
        {

            {
                if (StaticData.rushCurFloorNum <= limitFloorNum)
                {
                    return string.Format(TableTextBlo.GetText("rush_boss_floor_condition"), limitFloorNum);
                }
                else
                {
                    var pre = bossPage.GetPreRushEliteBossInfo(bossId);
                    if (pre != null) {
                        var db = Dict.Blo.DictRushBlo.GetTableRushEliteBoss(pre.eliteTaskId);
                        if (db != null)
                        {
                            return string.Format(TableTextBlo.GetText("rush_boss_defeat_condition"), TableTextBlo.GetText(db.EliteBossName));
                        }
                    }
                }
            }
            return "";
        }

        public void Select()
        {

            selector.SetActive(true);

        }

        public void UnSelect()
        {
            selector.SetActive(false);
        }

    }
}
